//
//  DR_RecommendedFileCell.swift
//  DocReaderRP
//
//  Created by  mac on 2025/11/14.
//

import UIKit
import SnapKit

class DR_RecommendedFileCell: UICollectionViewCell {
    
    static let identifier = "DR_RecommendedFileCell"
    
    private let thumbnailImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.backgroundColor = "#F6F7FF".toColor
        imageView.layer.cornerRadius = 20
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    private let fileNameLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        label.textColor = .black
        label.lineBreakMode = .byTruncatingMiddle
        label.textAlignment = .center
        return label
    }()
    
    private let dateLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = "#ADADAD".toColor
        label.textAlignment = .center
        return label
    }()
    
    private let fileImage: UIImageView = {
        let v = UIImageView()
        v.image = "home_file".toImage
        v.contentMode = .scaleAspectFill
        return v
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        setupConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.addSubview(thumbnailImageView)
        contentView.addSubview(fileNameLabel)
        contentView.addSubview(dateLabel)
        thumbnailImageView.addSubview(fileImage)
    }
    
    private func setupConstraints() {
        thumbnailImageView.snp.makeConstraints { make in
            make.top.leading.trailing.equalToSuperview()
            make.height.equalTo(148)
        }
        
        fileNameLabel.snp.makeConstraints { make in
            make.top.equalTo(thumbnailImageView.snp.bottom).offset(6)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(16)
        }
        
        dateLabel.snp.makeConstraints { make in
            make.top.equalTo(fileNameLabel.snp.bottom).offset(6)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(14)
        }
        
        fileImage.snp.makeConstraints { make in
            make.centerX.centerY.equalToSuperview()
            make.width.height.equalTo(60)
        }
    }
    
    func configure(with fileModel: DR_FileModel) {
        fileNameLabel.text = fileModel.displayName
        
        // 格式化日期
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let dateString = dateFormatter.string(from: fileModel.createDate)
        dateLabel.text = dateString
        
        if fileModel.fileType == .pdf {
            // 加载PDF预览图（异步）
            fileImage.isHidden = true
            loadThumbnail(for: fileModel.fileURL, fileType: fileModel.fileType)
        } else {
            // 其他文件类型显示默认图标
            fileImage.isHidden = false
            thumbnailImageView.image = nil
            thumbnailImageView.backgroundColor = "#F6F7FF".toColor
        }
    }
    
    private func loadThumbnail(for fileURL: URL, fileType: DR_FileType) {
        DispatchQueue.global(qos: .userInitiated).async { [weak self] in
            let thumbnail = DR_FileManager.shared.getFileThumbnail(fileURL: fileURL, fileType: fileType, size: CGSize(width: 200, height: 200))
            
            DispatchQueue.main.async {
                self?.thumbnailImageView.image = thumbnail
                if thumbnail != nil {
                    self?.fileImage.isHidden = true
                } else {
                    self?.fileImage.isHidden = false
                }
            }
        }
    }
}

